package controller;

import model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-29
 * Time：10:39
 */
@WebServlet("/takeoff")
public class TakeOffServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登陆已失效，请重新登陆！");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登陆已失效，请重新登陆！");
            return;
        }
        int uId = user.getUserId();
        String followingUserId = req.getParameter("userId");
        String blogId = req.getParameter("blogId");
        if (followingUserId != null) {
            int fId = Integer.parseInt(followingUserId);
            Following following = new Following();
            Follower follower = new Follower();
            following.setUserId(uId);
            following.setFollowingUserId(fId);
            follower.setUserId(fId);
            follower.setFollowerUserId(uId);

            FollowerDao followerDao = new FollowerDao();
            FollowingDao followingDao = new FollowingDao();
            followerDao.delete(follower);
            followingDao.delete(following);

            UserDao userDao = new UserDao();
            //将关注的对象从数据库中取出
            User followingUser =  userDao.selectById(fId);
            //更新user信息
            user = userDao.selectById(user.getUserId());

            //更新用户表里的followingCount和followerCount字段
            int followingOrigin = user.getFollowingCount();
            int followerOrigin = followingUser.getFollowerCount();
            //更新用户的关注数
            userDao.updateFollowingCount(uId, followingOrigin - 1);
            //更新作者的被关注数
            userDao.updateFollowerCount(fId, followerOrigin - 1);
            resp.sendRedirect("following_list.html?userId="+uId);
        } else {
            int bId = Integer.parseInt(blogId);
            Collection collection = new Collection();
            collection.setUserId(uId);
            collection.setBlogId(bId);
            //删除收藏数据
            CollectionDao collectionDao = new CollectionDao();
            collectionDao.delete(collection);
            //更新收藏数
            UserDao userDao = new UserDao();
            user = userDao.selectById(uId);
            int collectionOrigin = user.getCollectionCount();
            userDao.updateCollectionCount(uId, collectionOrigin - 1);
            resp.sendRedirect("collection_list.html");
        }
    }
}
